import android.util.Log
import com.example.flutter_app.utils.log
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

class FlutterAppMethodChannel(messenger: BinaryMessenger): MethodChannel.MethodCallHandler {

    private var channel: MethodChannel = MethodChannel(messenger, "com.flutterApp.MethodChannel")

    init {
        channel.setMethodCallHandler(this)
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when(call.method){
            "sendData"->{
                val name = call.argument("name") as String?
                val age = call.argument("age") as Int?
                var map = mapOf("name" to "hello,SunJH",
                        "age" to "$age",
                )
                Log.e("SunJH",map.toString())
                result.success(map)
            }
            "callFlutter"->{
                channel.invokeMethod("flutterMethod", listOf("第二个参数","第二个参数"),object :MethodChannel.Result{
                    override fun success(result: Any?) {
                        "flutterMethod:$result".log()
                    }
                    override fun error(errorCode: String?, errorMessage: String?, errorDetails: Any?) {
                            "调用发生错误".log()
                    }
                    override fun notImplemented() {
                        "未找到该方法的实现".log()
                    }
                })
                result.success("成功调用到android")
            }
            else ->{
                result.notImplemented()
            }
        }

    }
}
